package tree.sequentialstorage;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-04 20:23
 * @ Description :
 */
public class ArrayBinaryTree
{
    private int[] array;

    public ArrayBinaryTree(int[] array)
    {
        this.array = array;
    }

    public void preOrder()
    {
        this.preOrder(0);
    }

    //index: 根节点在数组中的索引
    public void preOrder(int index)
    {
        //可行性校验
        if (array == null || array.length == 0)
        {
            System.out.println("数组为空");
        }
        //1. 输出当前节点
        System.out.println(array[index]);

        //2. 左子树递归遍历
        if ((index * 2 + 1) < array.length)
        {
            preOrder(index * 2 + 1);
        }

        //3. 右子树递归遍历
        if ((index * 2 + 2) < array.length)
        {
            preOrder(index * 2 + 2);
        }
    }
}
